<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
// import { NDataTable, NButton, NCard, NInput, NGrid, NGi, NGridItem, NFlex, NVirtualList, NDialogProvider, useDialog, createDiscreteApi, treeDark, NModal, NFormItem,NSpace } from 'naive-ui'
import baseCard from '@/components/base-card/index.vue'
import baseTable from '@/components/base-table/index.vue'
import { useMenuRecordStore } from '@/stores/menuRecord'
import { useRouter } from 'vue-router'; 
import { useMeunItemStore } from '@/stores/meunList'
// import runScript from'../../assets/utils/js' 
const menuItem = useMeunItemStore()
// import {runScriptss }  from'../../assets/utils/mm'
// const { dialog } = createDiscreteApi(["dialog"])


const audioRef = ref<any>(null)
  const transcript = ref('');
const contentBoxRef = ref(null)

function name(params:any) {
  navigator.mediaDevices
        .getUserMedia({ audio: true })
        .then(mediaStream =>{
            // let  audio = document.querySelector("audioRef");
            audioRef.value.srcObject  = mediaStream;
            audioRef.value.play()
        

  let recognition: any;
 
  // 检查浏览器是否支持 Web Speech API
  if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) {
    console.log('jdsfhjasdhjka');
    
    recognition = new (window as any).webkitSpeechRecognition || new (window as any).SpeechRecognition();
    recognition.start()
    recognition.lang = 'zh-CN';  // 设置识别语言
    recognition.continuous = true;  // 是否连续识别
    recognition.interimResults = true;  // 是否返回中间结果
  
    recognition.onsoundstart = true
    recognition.onspeechstart = true

    console.log(recognition);

    // 识别结果事件
    recognition.onresult = (event: any) => {
      console.log(event);
      
      transcript.value = '';  // 清空之前的文本
      for (let i = 0; i < event.results.length; ++i) {
        transcript.value += event.results[i][0].transcript;  // 拼接识别结果
      }
    };
 
    // 识别错误事件
    recognition.onerror = (event: any) => {
      console.error('识别出错:', event.error);
    };
 
    // 识别结束事件
    recognition.onend = () => {
      audioRef.value.pause()
      recognition.stop()
      console.log('识别结束');
    };
  } else {
    console.error('当前浏览器不支持语音识别');
  }

})
        .catch(err => {
            console.log(err.name + ": " + err.message);
        });
}

function callNodeScript() {
  
}
function setmeuns() {
  // localStorage.setItem('meunItem','kkkkkkk')

  let a=[{
    id:'kmcdxzkldslaslk',
    name1:'jjjjjjj'
  },
  {
    id:'qweqewqewq',
    name1:'rrrrrrrr'
  }]


  menuItem.setMeunlist(a[1])
}


</script>
<template>
  <baseCard>
    <!-- <div>
         <audio controls ref="audioRef">
                您的浏览器不支持 audio 元素
            </audio>
            
 
  </div>
  <input type="button" value="asadsads" @click="name">
  <div style="border: solid 1px red ;height: 300px;color: yellow;">
    {{ transcript }}
  </div> -->
  <button @click="callNodeScript">Call Node.js Script</button>

  <button @click="setmeuns">setmeun</button>
</baseCard>
</template>

<style src="./index.scss" scoped></style>
<style lang="scss" scoped>

</style>
